package com.lemon.exam.controller;

import com.lemon.exam.common.annotation.Web;
import com.lemon.exam.common.crud.BaseController;
import com.lemon.exam.common.entity.vo.SSEMessageVO;
import com.lemon.exam.common.entity.vo.TreeSelectVO;
import com.lemon.exam.entity.po.PaperPO;
import com.lemon.exam.entity.vo.PaperQuestionVO;
import com.lemon.exam.service.IPaperService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.MediaType;
import org.springframework.http.codec.multipart.FilePart;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.List;
import java.util.Map;

/**
 * 试卷表 控制器
 *
 * @author Lemon
 * @since 2025/05/11 13:47:13
 */
@RestController
@RequestMapping("paper")
@Tag(name = "试卷表管理", description = "试卷表 API")
@Web(childUri = "paper", childComponent = "system/Paper.vue")
public class PaperController extends BaseController<PaperPO, Long, IPaperService> {
    /**
     * 上传
     *
     * @param filePart
     * @return
     */
    @PostMapping(value = "upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @Operation(summary = "上传试卷")
    public Mono<Map<String, Object>> upload(@RequestPart("file") FilePart filePart) {
        return service.upload(filePart);
    }

    /**
     * 查询所有的班级
     *
     * @return
     */
    @GetMapping(value = "findClassSelect")
    @Operation(summary = "查询所有的班级")
    public Flux<TreeSelectVO> findClassSelect() {
        return service.findClassSelect();
    }

    /**
     * 导出模板
     *
     * @param response
     */
    @PostMapping("exportTemplate")
    @Operation(summary = "导出模板")
    public Mono<Void> exportTemplate(ServerHttpResponse response) {
        return service.exportTemplate(response);
    }

    /**
     * 下载试题
     *
     * @return
     */
    @PostMapping(value = "download/{id}")
    @Operation(summary = "下载试题")
    public Mono<Void> download(@PathVariable("id") Long id, ServerHttpResponse response) {
        return service.download(id, response);
    }

    /**
     * 查看试题
     *
     * @return
     */
    @GetMapping(value = "findPaperQuestion/{id}")
    @Operation(summary = "查看试题")
    public Mono<List<PaperQuestionVO>> findPaperQuestion(@PathVariable("id") Long id) {
        return service.findPaperQuestion(id);
    }

    /**
     * AI生成试题
     *
     * @param param
     * @param token
     * @return
     */
    @PostMapping(value = "generatePaper")
    @Operation(summary = "AI生成")
    public Flux<SSEMessageVO> generatePaper(@RequestBody Map<String, Object> param, @RequestHeader("Authorization") String token) {
        return service.generatePaper(param, token);
    }
}
